package com.dearbinge.opentapi.content.life;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.life.LifeMessageService;
import com.dearbinge.data.api.moments.BusinessCircleService;
import com.dearbinge.data.pojo.life.LifeMessage;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


/**
 * 生活圈控制层
 *
 * @author wes
 * @date 18:22 2019/1/4
 */
@Api(tags = "1.3、社交圈-生活动态")
@RestController
@RequestMapping("/life")
@EnableAutoConfiguration
public class LifeMessageController extends WebMvcConfigurerAdapter {

    @Autowired
    private LifeMessageService lifeMessageService;
    @Autowired
    private BusinessCircleService businessCircleService;
    /*————————————————————————废弃的接口 新版本上线后删除————————————————————————————————————*/

    /**
     * 生活信息增加 修改
     * @param lifeMessage 生活信息
     * @return 结果提示
     */
    @ApiOperation(value = "生活信息：新增（id为空||id为0）/修改(废弃)", notes = "生活信息：新增（id为空||id为0）/修改(废弃)")
    @RequestMapping(value = "/addOrUpdateLifeMessage", method = RequestMethod.POST)
    public ResultMsg<String> addOrUpdateLifeMessage(@RequestBody LifeMessage lifeMessage) {
        try {
            Integer dataCode ;
            if (lifeMessage.getId() != null && lifeMessage.getId() != 0) {
                if (StringUtils.isBlank(lifeMessage.getContent())) {
                    return ResultMsg.error(500,"内容不能为空");
                }
                dataCode = lifeMessageService.updateLifeMessage(lifeMessage);
                if (dataCode > 1) {
                    return ResultMsg.ok("修改成功");
                }
            } else {
                dataCode = lifeMessageService.insertLifeMessage(lifeMessage);
                if (dataCode > 1) {
                    return ResultMsg.ok("添加成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }



    /**
     *  查询一个用户发布的生活信息
     * @param userId 用户id
     * @param current 当前页
     * @param size 页面记录数
     * @return 分页结果
     */
    @ApiOperation(value = "查询一个用户发布的生活信息(废弃)", notes = "查询一个用户发布的生活信息(废弃)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query",required=true),
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页面记录数", paramType = "query")})
    @RequestMapping(value = "/listLiveMoments", method = RequestMethod.GET)
    public ResultMsg<Page> listLiveMoments(String userId, Integer current, Integer size) {
        try {
            Page page = businessCircleService.listAllMomentsByUserId(userId, current, size,2);
            return ResultMsg.ok(page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }



    /**
     * 根据用户id和生活信息id查询生活信息动态
     * @author wes
     * @date 18:56 2019/1/4
     */
 /*   @ApiOperation(value = "根据用户id和id查询生活信息动态", notes = "根据用户id和id查询生活信息动态")
    @RequestMapping(value = "/getLifeMessage", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query", required = true),
            @ApiImplicitParam(name = "id", value = "主键id", paramType = "query", required = true)})
    public ResultMsg<Map> getLifeMessage(String userId, Integer id) {
        try {
            Map result = lifeMessageService.getLifeMessage(userId,id);
            return ResultMsg.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }*/
}
